﻿//////////////////////////////////////////////////////////////////////////
//
// yikaiming (C) 2013
// gkENGINE Source File 
//
// Name:   	BufferObject.h
// Desc:	
// 	
// 
// Author:  YiKaiming
// Date:	2013/9/17
// 
//////////////////////////////////////////////////////////////////////////

#ifndef _BufferObject_h_
#define _BufferObject_h_

#include "prerequisite.h"

class CVertexBuffer
{
public:
	CVertexBuffer( IDirect3DDevice9* device, int bufferSize ):m_device(device)
	{
		m_device->CreateVertexBuffer( bufferSize, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT, &m_vertexBuffer, NULL );
	}

	~CVertexBuffer()
	{
		m_vertexBuffer->Release();
	}

	void Update(void* data, int length)
	{
		void* gpuData = NULL;
		if( D3D_OK == m_vertexBuffer->Lock( 0, sizeof(float) * 9, &gpuData, NULL ) )
		{
			memcpy( gpuData, data, length );
			m_vertexBuffer->Unlock();
		}
		else
		{
			MessageBox(NULL, "vb create failed", "shader compile failed", MB_OK);
		}
	}

	void Use()
	{
		m_device->SetStreamSource( 0, m_vertexBuffer, 0, sizeof(float) * 3);
	}

	IDirect3DDevice9* m_device;
	IDirect3DVertexBuffer9* m_vertexBuffer;
};

#endif